<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>merge</title>
<link rel="stylesheet" href="../../../../../../../../doc/src/boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../../../../index.html" title="Chapter 1. Range 2.0">
<link rel="up" href="../mutating.html" title="Mutating algorithms">
<link rel="prev" href="inplace_merge.html" title="inplace_merge">
<link rel="next" href="nth_element.html" title="nth_element">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../../../boost.png"></td>
<td align="center"><a href="../../../../../../../../index.html">Home</a></td>
<td align="center"><a href="../../../../../../../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../../../../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="inplace_merge.html"><img src="../../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../mutating.html"><img src="../../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../../index.html"><img src="../../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="nth_element.html"><img src="../../../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h5 class="title">
<a name="range.reference.algorithms.mutating.merge"></a><a class="link" href="merge.html" title="merge">merge</a>
</h5></div></div></div>
<h6>
<a name="range.reference.algorithms.mutating.merge.h0"></a>
            <span class="phrase"><a name="range.reference.algorithms.mutating.merge.prototype"></a></span><a class="link" href="merge.html#range.reference.algorithms.mutating.merge.prototype">Prototype</a>
          </h6>
<p>
</p>
<pre class="programlisting"><span class="keyword">template</span><span class="special">&lt;</span>
    <span class="keyword">class</span> <span class="identifier">SinglePassRange1</span><span class="special">,</span>
    <span class="keyword">class</span> <span class="identifier">SinglePassRange2</span><span class="special">,</span>
    <span class="keyword">class</span> <span class="identifier">OutputIterator</span>
    <span class="special">&gt;</span>
<span class="identifier">OutputIterator</span> <span class="identifier">merge</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">SinglePassRange1</span><span class="special">&amp;</span> <span class="identifier">rng1</span><span class="special">,</span>
                     <span class="keyword">const</span> <span class="identifier">SinglePassRange2</span><span class="special">&amp;</span> <span class="identifier">rng2</span><span class="special">,</span>
                     <span class="identifier">OutputIterator</span>          <span class="identifier">out</span><span class="special">);</span>

<span class="keyword">template</span><span class="special">&lt;</span>
    <span class="keyword">class</span> <span class="identifier">SinglePassRange1</span><span class="special">,</span>
    <span class="keyword">class</span> <span class="identifier">SinglePassRange2</span><span class="special">,</span>
    <span class="keyword">class</span> <span class="identifier">OutputIterator</span><span class="special">,</span>
    <span class="keyword">class</span> <span class="identifier">BinaryPredicate</span>
    <span class="special">&gt;</span>
<span class="identifier">OutputIterator</span> <span class="identifier">merge</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">SinglePassRange1</span><span class="special">&amp;</span> <span class="identifier">rng1</span><span class="special">,</span>
                     <span class="keyword">const</span> <span class="identifier">SinglePassRange2</span><span class="special">&amp;</span> <span class="identifier">rng2</span><span class="special">,</span>
                     <span class="identifier">OutputIterator</span>          <span class="identifier">out</span><span class="special">,</span>
                     <span class="identifier">BinaryPredicate</span>         <span class="identifier">pred</span><span class="special">);</span>
</pre>
<p>
          </p>
<h6>
<a name="range.reference.algorithms.mutating.merge.h1"></a>
            <span class="phrase"><a name="range.reference.algorithms.mutating.merge.description"></a></span><a class="link" href="merge.html#range.reference.algorithms.mutating.merge.description">Description</a>
          </h6>
<p>
            <code class="computeroutput"><span class="identifier">merge</span></code> combines two sorted
            ranges <code class="computeroutput"><span class="identifier">rng1</span></code> and <code class="computeroutput"><span class="identifier">rng2</span></code> into a single sorted range by
            copying elements. <code class="computeroutput"><span class="identifier">merge</span></code>
            is stable. The return value is <code class="computeroutput"><span class="identifier">out</span>
            <span class="special">+</span> <span class="identifier">distance</span><span class="special">(</span><span class="identifier">rng1</span><span class="special">)</span> <span class="special">+</span> <span class="identifier">distance</span><span class="special">(</span><span class="identifier">rng2</span><span class="special">)</span></code>.
          </p>
<p>
            The two versions of <code class="computeroutput"><span class="identifier">merge</span></code>
            differ by how they compare the elements.
          </p>
<p>
            The non-predicate version uses the <code class="computeroutput"><span class="keyword">operator</span><span class="special">&lt;()</span></code> for the range value type. The predicate
            version uses the predicate instead of <code class="computeroutput"><span class="keyword">operator</span><span class="special">&lt;()</span></code>.
          </p>
<h6>
<a name="range.reference.algorithms.mutating.merge.h2"></a>
            <span class="phrase"><a name="range.reference.algorithms.mutating.merge.definition"></a></span><a class="link" href="merge.html#range.reference.algorithms.mutating.merge.definition">Definition</a>
          </h6>
<p>
            Defined in the header file <code class="computeroutput"><span class="identifier">boost</span><span class="special">/</span><span class="identifier">range</span><span class="special">/</span><span class="identifier">algorithm</span><span class="special">/</span><span class="identifier">merge</span><span class="special">.</span><span class="identifier">hpp</span></code>
          </p>
<h6>
<a name="range.reference.algorithms.mutating.merge.h3"></a>
            <span class="phrase"><a name="range.reference.algorithms.mutating.merge.requirements"></a></span><a class="link" href="merge.html#range.reference.algorithms.mutating.merge.requirements">Requirements</a>
          </h6>
<p>
            <span class="bold"><strong>For the non-predicate version:</strong></span>
          </p>
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
<li class="listitem">
                <code class="computeroutput"><span class="identifier">SinglePassRange1</span></code>
                is a model of the <a class="link" href="../../../concepts/single_pass_range.html" title="Single Pass Range">Single
                Pass Range</a> Concept.
              </li>
<li class="listitem">
                <code class="computeroutput"><span class="identifier">SinglePassRange2</span></code>
                is a model of the <a class="link" href="../../../concepts/single_pass_range.html" title="Single Pass Range">Single
                Pass Range</a> Concept.
              </li>
<li class="listitem">
                <code class="computeroutput"><span class="identifier">range_value</span><span class="special">&lt;</span><span class="identifier">SinglePassRange1</span><span class="special">&gt;::</span><span class="identifier">type</span></code> is the same as <code class="computeroutput"><span class="identifier">range_value</span><span class="special">&lt;</span><span class="identifier">SinglePassRange2</span><span class="special">&gt;::</span><span class="identifier">type</span></code>.
              </li>
<li class="listitem">
                <code class="computeroutput"><span class="identifier">range_value</span><span class="special">&lt;</span><span class="identifier">SinglePassRange1</span><span class="special">&gt;::</span><span class="identifier">type</span></code> is a model of the <code class="computeroutput"><span class="identifier">LessThanComparableConcept</span></code>.
              </li>
<li class="listitem">
                The ordering on objects of <code class="computeroutput"><span class="identifier">range_value</span><span class="special">&lt;</span><span class="identifier">SinglePassRange1</span><span class="special">&gt;::</span><span class="identifier">type</span></code>
                is a <span class="bold"><strong><span class="emphasis"><em>strict weak ordering</em></span></strong></span>,
                as defined in the <code class="computeroutput"><span class="identifier">LessThanComparableConcept</span></code>
                requirements.
              </li>
<li class="listitem">
                <code class="computeroutput"><span class="identifier">range_value</span><span class="special">&lt;</span><span class="identifier">SinglePassRange1</span><span class="special">&gt;::</span><span class="identifier">type</span></code> is convertible to a type in
                <code class="computeroutput"><span class="identifier">OutputIterator</span></code>'s
                set of value types.
              </li>
</ul></div>
<p>
            <span class="bold"><strong>For the predicate version:</strong></span>
          </p>
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
<li class="listitem">
                <code class="computeroutput"><span class="identifier">SinglePassRange1</span></code>
                is a model of the <a class="link" href="../../../concepts/single_pass_range.html" title="Single Pass Range">Single
                Pass Range</a> Concept.
              </li>
<li class="listitem">
                <code class="computeroutput"><span class="identifier">SinglePassRange2</span></code>
                is a model of the <a class="link" href="../../../concepts/single_pass_range.html" title="Single Pass Range">Single
                Pass Range</a> Concept.
              </li>
<li class="listitem">
                <code class="computeroutput"><span class="identifier">range_value</span><span class="special">&lt;</span><span class="identifier">SinglePassRange1</span><span class="special">&gt;::</span><span class="identifier">type</span></code> is the same as <code class="computeroutput"><span class="identifier">range_value</span><span class="special">&lt;</span><span class="identifier">SinglePassRange2</span><span class="special">&gt;::</span><span class="identifier">type</span></code>.
              </li>
<li class="listitem">
                <code class="computeroutput"><span class="identifier">BinaryPredicate</span></code> is
                a model of the <code class="computeroutput"><span class="identifier">StrictWeakOrderingConcept</span></code>.
              </li>
<li class="listitem">
                <code class="computeroutput"><span class="identifier">SinglePassRange1</span></code>'s
                value type is convertible to both <code class="computeroutput"><span class="identifier">BinaryPredicate</span></code>'s
                argument types.
              </li>
<li class="listitem">
                <code class="computeroutput"><span class="identifier">range_value</span><span class="special">&lt;</span><span class="identifier">SinglePassRange1</span><span class="special">&gt;::</span><span class="identifier">type</span></code> is convertible to a type in
                <code class="computeroutput"><span class="identifier">OutputIterator</span></code>'s
                set of value types.
              </li>
</ul></div>
<h6>
<a name="range.reference.algorithms.mutating.merge.h4"></a>
            <span class="phrase"><a name="range.reference.algorithms.mutating.merge.precondition_"></a></span><a class="link" href="merge.html#range.reference.algorithms.mutating.merge.precondition_">Precondition:</a>
          </h6>
<h6>
<a name="range.reference.algorithms.mutating.merge.h5"></a>
            <span class="phrase"><a name="range.reference.algorithms.mutating.merge.for_the_non_predicate_version_"></a></span><a class="link" href="merge.html#range.reference.algorithms.mutating.merge.for_the_non_predicate_version_">For
            the non-predicate version:</a>
          </h6>
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
<li class="listitem">
                The elements of <code class="computeroutput"><span class="identifier">rng1</span></code>
                are in ascending order. That is, for each adjacent element pair
                <code class="computeroutput"><span class="special">[</span><span class="identifier">x</span><span class="special">,</span><span class="identifier">y</span><span class="special">]</span></code> of <code class="computeroutput"><span class="identifier">rng1</span></code>,
                <code class="computeroutput"><span class="identifier">y</span> <span class="special">&lt;</span>
                <span class="identifier">x</span> <span class="special">==</span>
                <span class="keyword">false</span></code>.
              </li>
<li class="listitem">
                The elements of <code class="computeroutput"><span class="identifier">rng2</span></code>
                are in ascending order. That is, for each adjacent element pair
                <code class="computeroutput"><span class="special">[</span><span class="identifier">x</span><span class="special">,</span><span class="identifier">y</span><span class="special">]</span></code> of <code class="computeroutput"><span class="identifier">rng2</span></code>,
                <code class="computeroutput"><span class="identifier">y</span> <span class="special">&lt;</span>
                <span class="identifier">x</span> <span class="special">==</span>
                <span class="keyword">false</span></code>.
              </li>
<li class="listitem">
                The ranges <code class="computeroutput"><span class="identifier">rng1</span></code> and
                <code class="computeroutput"><span class="special">[</span><span class="identifier">out</span><span class="special">,</span> <span class="identifier">out</span>
                <span class="special">+</span> <span class="identifier">distance</span><span class="special">(</span><span class="identifier">rng1</span><span class="special">)</span> <span class="special">+</span> <span class="identifier">distance</span><span class="special">(</span><span class="identifier">rng2</span><span class="special">))</span></code>
                do not overlap.
              </li>
<li class="listitem">
                The ranges <code class="computeroutput"><span class="identifier">rng2</span></code> and
                <code class="computeroutput"><span class="special">[</span><span class="identifier">out</span><span class="special">,</span> <span class="identifier">out</span>
                <span class="special">+</span> <span class="identifier">distance</span><span class="special">(</span><span class="identifier">rng1</span><span class="special">)</span> <span class="special">+</span> <span class="identifier">distance</span><span class="special">(</span><span class="identifier">rng2</span><span class="special">))</span></code>
                do not overlap.
              </li>
<li class="listitem">
                <code class="computeroutput"><span class="special">[</span><span class="identifier">out</span><span class="special">,</span> <span class="identifier">out</span>
                <span class="special">+</span> <span class="identifier">distance</span><span class="special">(</span><span class="identifier">rng1</span><span class="special">)</span> <span class="special">+</span> <span class="identifier">distance</span><span class="special">(</span><span class="identifier">rng2</span><span class="special">))</span></code>
                is a valid range.
              </li>
</ul></div>
<h6>
<a name="range.reference.algorithms.mutating.merge.h6"></a>
            <span class="phrase"><a name="range.reference.algorithms.mutating.merge.for_the_predicate_version_"></a></span><a class="link" href="merge.html#range.reference.algorithms.mutating.merge.for_the_predicate_version_">For
            the predicate version:</a>
          </h6>
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
<li class="listitem">
                The elements of <code class="computeroutput"><span class="identifier">rng1</span></code>
                are in ascending order. That is, for each adjacent element pair
                <code class="computeroutput"><span class="special">[</span><span class="identifier">x</span><span class="special">,</span><span class="identifier">y</span><span class="special">]</span></code>, of <code class="computeroutput"><span class="identifier">rng1</span></code>,
                <code class="computeroutput"><span class="identifier">pred</span><span class="special">(</span><span class="identifier">y</span><span class="special">,</span> <span class="identifier">x</span><span class="special">)</span> <span class="special">==</span> <span class="keyword">false</span></code>.
              </li>
<li class="listitem">
                The elements of <code class="computeroutput"><span class="identifier">rng2</span></code>
                are in ascending order. That is, for each adjacent element pair
                <code class="computeroutput"><span class="special">[</span><span class="identifier">x</span><span class="special">,</span><span class="identifier">y</span><span class="special">]</span></code>, of <code class="computeroutput"><span class="identifier">rng2</span></code>,
                <code class="computeroutput"><span class="identifier">pred</span><span class="special">(</span><span class="identifier">y</span><span class="special">,</span> <span class="identifier">x</span><span class="special">)</span> <span class="special">==</span> <span class="keyword">false</span></code>.
              </li>
<li class="listitem">
                The ranges <code class="computeroutput"><span class="identifier">rng1</span></code> and
                <code class="computeroutput"><span class="special">[</span><span class="identifier">out</span><span class="special">,</span> <span class="identifier">out</span>
                <span class="special">+</span> <span class="identifier">distance</span><span class="special">(</span><span class="identifier">rng1</span><span class="special">)</span> <span class="special">+</span> <span class="identifier">distance</span><span class="special">(</span><span class="identifier">rng2</span><span class="special">))</span></code>
                do not overlap.
              </li>
<li class="listitem">
                The ranges <code class="computeroutput"><span class="identifier">rng2</span></code> and
                <code class="computeroutput"><span class="special">[</span><span class="identifier">out</span><span class="special">,</span> <span class="identifier">out</span>
                <span class="special">+</span> <span class="identifier">distance</span><span class="special">(</span><span class="identifier">rng1</span><span class="special">)</span> <span class="special">+</span> <span class="identifier">distance</span><span class="special">(</span><span class="identifier">rng2</span><span class="special">))</span></code>
                do not overlap.
              </li>
<li class="listitem">
                <code class="computeroutput"><span class="special">[</span><span class="identifier">out</span><span class="special">,</span> <span class="identifier">out</span>
                <span class="special">+</span> <span class="identifier">distance</span><span class="special">(</span><span class="identifier">rng1</span><span class="special">)</span> <span class="special">+</span> <span class="identifier">distance</span><span class="special">(</span><span class="identifier">rng2</span><span class="special">))</span></code>
                is a valid range.
              </li>
</ul></div>
<h6>
<a name="range.reference.algorithms.mutating.merge.h7"></a>
            <span class="phrase"><a name="range.reference.algorithms.mutating.merge.complexity"></a></span><a class="link" href="merge.html#range.reference.algorithms.mutating.merge.complexity">Complexity</a>
          </h6>
<p>
            Linear. There are no comparisons if both <code class="computeroutput"><span class="identifier">rng1</span></code>
            and <code class="computeroutput"><span class="identifier">rng2</span></code> are empty, otherwise
            at most <code class="computeroutput"><span class="identifier">distance</span><span class="special">(</span><span class="identifier">rng1</span><span class="special">)</span> <span class="special">+</span> <span class="identifier">distance</span><span class="special">(</span><span class="identifier">rng2</span><span class="special">)</span> <span class="special">-</span> <span class="number">1</span></code>
            comparisons.
          </p>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright © 2003-2010 Thorsten Ottosen,
      Neil Groves<p>
        Distributed under the Boost Software License, Version 1.0. (See accompanying
        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
      </p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="inplace_merge.html"><img src="../../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../mutating.html"><img src="../../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../../index.html"><img src="../../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="nth_element.html"><img src="../../../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
</body>
</html>
